package org.usts.trainning.supermarket.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.usts.trainning.supermarket.dao.UserDao;
import org.usts.trainning.supermarket.entity.User;

public class UserContribute {
	public String findUserByName(HttpServletRequest request, HttpServletResponse response) {
		UserDao dao = new UserDao();
		String uname = request.getParameter("uname");
		String password = request.getParameter("password");
		User user = dao.findUserByName(uname);
		boolean flag = false;
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		if (user != null) {
			if ((uname.equals(user.getUname())) && (password.equals(user.getPassword()))) {
				flag = true;
			} else {
				flag = false;
			}
		}
		if (flag) {
			HttpSession session = request.getSession();
			session.setAttribute("loginuser", user);
			return "/index.jsp";
		} else {
			request.setAttribute("loginMeg", "用户名或密码错误！");
			return "/login.jsp";
		}

	}

	// 获取所有用户
	public String getAllUser(HttpServletRequest request, HttpServletResponse response) {
		UserDao dao = new UserDao();
		List<User> list = dao.findAllUser();
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		request.setAttribute("listalluser", list);
		return "/userList.jsp";
	}

	//
	public String findUser(HttpServletRequest request, HttpServletResponse response) {
		UserDao dao = new UserDao();
		String uname = request.getParameter("uname");
		User user = dao.findUserByName(uname);
		dao.close();
		request.setAttribute("lookuser", user);
		return "/userView.jsp";

	}

	public String updateUser(HttpServletRequest request, HttpServletResponse response) {
		UserDao dao = new UserDao();
		String uname = request.getParameter("uname");
		User user = dao.findUserByName(uname);
		dao.close();
		request.setAttribute("updateuser", user);
		return "/userUpdate.jsp";

	}

	// 修改用户
	public String updateUserById(HttpServletRequest request, HttpServletResponse response) {
		UserDao dao = new UserDao();
		User user = new User();
		String uid = request.getParameter("uid");
		int id = Integer.parseInt(uid);
		user.setUid(id);
		user.setUname(request.getParameter("uname"));
		String uage = request.getParameter("uage");
		int age = Integer.parseInt(uage);
		user.setUage(age);
		user.setUsex(request.getParameter("usex"));
		String phone = request.getParameter("phone");
		user.setPhone(phone);
		user.setPaddress(request.getParameter("paddress"));
		user.setUserRole(request.getParameter("userRole"));
		boolean result = dao.updateUserById(user);
		System.out.println(result);

		if (result) {
			request.setAttribute("edit", user);
			getAllUser(request, response);
			return "/userList.jsp";
		} else {
			return null;
		}

	}

	// 增加用户
	public String addUser(HttpServletRequest request, HttpServletResponse response) {
		UserDao dao = new UserDao();
		User user = new User();
		user.setUcode(request.getParameter("ucode"));
		user.setUname(request.getParameter("uname"));
		user.setPassword(request.getParameter("password"));
		user.setUsex(request.getParameter("usex"));
		user.setUage(Integer.parseInt(request.getParameter("uage")));
		user.setPhone(request.getParameter("phone"));
		user.setPaddress(request.getParameter("paddress"));
		user.setUserRole(request.getParameter("userRole"));

		boolean result = dao.addUser(user);
		dao.close();
		if (result) {
			getAllUser(request, response);
			request.setAttribute("adduser", user);
			return "/userList.jsp";
		} else {
			return null;
		}
	}

	public String deleteUser(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("uid");
		int uid = Integer.parseInt(id);
		UserDao dao = new UserDao();
		boolean flag = dao.deleteUser(uid);
		List<User> c = dao.findAllUser();
		dao.close();
		if (flag) {
			request.setAttribute("listalluser", c);
			return "/userList.jsp";
		} else {
			return null;
		}

	}

	public String modifyPwd(HttpServletRequest request, HttpServletResponse response) {
		UserDao dao = new UserDao();
		User user = new User();
		String newPassword = request.getParameter("newPassword");
		String reNewPassword = request.getParameter("reNewPassword");
		if (newPassword.equals(reNewPassword)) {
			user.setUid(Integer.parseInt(request.getParameter("uid")));
			user.setPassword(newPassword);
			boolean result = dao.modifyPwdById(user);
			dao.close();
			request.setAttribute("pwdmes", "修改成功");
			return "/password.jsp";
		} else {
			request.setAttribute("pwdmes", "修改失败");
			return "/password.jsp";
		}

	}

	public String searchUser(HttpServletRequest request, HttpServletResponse response) {
		UserDao dao = new UserDao();
		String uname = request.getParameter("uname");
		if (uname == null) {
			System.out.println("unme====nullll");
		}
		List<User> list = dao.findAllUser(uname);
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		request.setAttribute("listalluser", list);
		return "/userList.jsp";
	}

	public String eixt(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		session.invalidate();
		return "/login.jsp";
	}
}